<?php


namespace app\controller\archives;
require_once __DIR__.'/../../common/AuthController.php';
use app\common\AuthController;


/**
 * PC后端设备档案
 */
class Equipment extends AuthController
{   


    /**
     * 初始化操作
     */
    public function init()
    {

    }
	

    /**
     * 获取设备列表
     */
	public function api_archives_equipment_getList($msg)
    {   
        global $db;
        $info = [
            'page' =>  isset($msg['data']['page'])?$msg['data']['page']:1,
            'rows' => isset($msg['data']['rows'])?$msg['data']['rows']:15,
            'id' => isset($msg['data']['id'])?$msg['data']['id']:'',
            'name' => isset($msg['data']['name'])?$msg['data']['name']:'',
            'attribute' => isset($msg['data']['attribute'])?$msg['data']['attribute']:'',
            'category_name' => isset($msg['data']['category_name'])?$msg['data']['category_name']:'',
            'brand_name' => isset($msg['data']['brand_name'])?$msg['data']['brand_name']:'',
            'supplier_name' => isset($msg['data']['supplier_name'])?$msg['data']['supplier_name']:'',
        ];
        $limit = ($info['page']-1)*$info['rows'].','.$info['rows'];
        $where = "is_delete = 0";
        if(!empty($info['id'])){
            $where .= " AND id =".$info['id'];
        }
        if(!empty($info['name'])){
            $where .= " AND name LIKE '%".$info['name']."%'";
        }
        if(!empty($info['brand_name'])){
            $where .= " AND brand_name LIKE '%".$info['brand_name']."%'";
        }
        if(!empty($info['attribute'])){
            $where .= " AND attribute LIKE '%".$info['attribute']."%'";
        }
        if(!empty($info['category_name'])){
            $where .= " AND category_name LIKE '%".$info['category_name']."%'";
        }
        if(!empty($info['supplier_name'])){
            $where .= " AND supplier_name LIKE '%".$info['supplier_name']."%'";
        }
		$list = $db->query("select * from equipment where ".$where ." order by id  desc limit ".$limit); 
        $count =  $db->query("SELECT count(*) as count from equipment WHERE ".$where);
        return $this->outMessage(10000,'获取成功',$list,'success',$count[0]['count']);
	}



    /**
     * 添加
     */
    public function api_archives_equipment_add($msg)
    {   
        $info = [
            'name' =>  isset($msg['data']['name'])?$msg['data']['name']:'',
            'category_id' => isset($msg['data']['category_id'])?$msg['data']['category_id']:'',
            'category_name' => isset($msg['data']['category_name'])?$msg['data']['category_name']:'',
            'attribute' => isset($msg['data']['attribute'])?$msg['data']['attribute']:'',
            'brand_id' =>  isset($msg['data']['brand_id'])?$msg['data']['brand_id']:'',
            'brand_name' => isset($msg['data']['brand_name'])?$msg['data']['brand_name']:'',
            'buy_price' => isset($msg['data']['buy_price'])?$msg['data']['buy_price']:'',
            'price' => isset($msg['data']['price'])?$msg['data']['price']:'',
            'supplier_id' =>  isset($msg['data']['supplier_id'])?$msg['data']['supplier_id']:'',
            'supplier_name' => isset($msg['data']['supplier_name'])?$msg['data']['supplier_name']:'',
        ];
        foreach ($info as $v) {
            if(empty($v)){
                return $this->outMessage(-1,'参数不能为空');
            }
        }
        $info['pathway_number'] = isset($msg['data']['pathway_number'])?$msg['data']['pathway_number']:0;
        $info['gross_number'] = isset($msg['data']['gross_number'])?$msg['data']['gross_number']:0;
        if(!$this->validate->checkName($info['name'])){
            return $this->outMessage(-1,'请输入正确的名称');
        }
        $res = $this->table->find('equipment',['name'=>$info['name']]);
        if($res){
            return $this->outMessage(-1,'该名称已存在,不可重复');
        }
        $data = [
            'name' => $info['name'],
            'category_id' => $info['category_id'],
            'category_name' => $info['category_name'],
            'attribute' => $info['attribute'],
            'brand_id' => $info['brand_id'],
            'brand_name' => $info['brand_name'],
            'buy_price' => $info['buy_price'],
            'price' => $info['price'],
            'supplier_id' => $info['supplier_id'],
            'supplier_name' => $info['supplier_name'],
            'pathway_number' => $info['pathway_number'],
            'gross_number' => $info['gross_number'],
            'create_time' => date('Y-m-d H:i:s',time())
        ];
        $res = $this->table->insert('equipment',$data);
        if(!$res){
            return $this->outMessage(-1,'添加失败');
        }
        return $this->outMessage(10000,'添加成功');
    }


    
    /**
     * 编辑
     */
    public function api_archives_equipment_edit($msg)
    {   
        $info = [
            'id' =>  isset($msg['data']['id'])?$msg['data']['id']:'',
            'name' =>  isset($msg['data']['name'])?$msg['data']['name']:'',
            'category_id' => isset($msg['data']['category_id'])?$msg['data']['category_id']:'',
            'category_name' => isset($msg['data']['category_name'])?$msg['data']['category_name']:'',
            'attribute' => isset($msg['data']['attribute'])?$msg['data']['attribute']:'',
            'brand_id' =>  isset($msg['data']['brand_id'])?$msg['data']['brand_id']:'',
            'brand_name' => isset($msg['data']['brand_name'])?$msg['data']['brand_name']:'',
            'buy_price' => isset($msg['data']['buy_price'])?$msg['data']['buy_price']:'',
            'price' => isset($msg['data']['price'])?$msg['data']['price']:'',
            'supplier_id' =>  isset($msg['data']['supplier_id'])?$msg['data']['supplier_id']:'',
            'supplier_name' => isset($msg['data']['supplier_name'])?$msg['data']['supplier_name']:'',
        ];
        foreach ($info as $v) {
            if(empty($v)){
                return $this->outMessage(-1,'参数不能为空');
            }
        }
        $info['pathway_number'] = isset($msg['data']['pathway_number'])?$msg['data']['pathway_number']:0;
        $info['gross_number'] = isset($msg['data']['gross_number'])?$msg['data']['gross_number']:0;
        if(!$this->validate->checkName($info['name'])){
            return $this->outMessage(-1,'请输入正确的名称');
        }
        $res = $this->table->find('equipment',['name'=>$info['name']]);
        if(!empty($res) && $res['id'] != $info['id']){
            return $this->outMessage(-1,'该名称已存在,不可重复');
        }
        $data = [
            'name' => $info['name'],
            'category_id' => $info['category_id'],
            'category_name' => $info['category_name'],
            'attribute' => $info['attribute'],
            'brand_id' => $info['brand_id'],
            'brand_name' => $info['brand_name'],
            'buy_price' => $info['buy_price'],
            'price' => $info['price'],
            'supplier_id' => $info['supplier_id'],
            'supplier_name' => $info['supplier_name'],
            'pathway_number' => $info['pathway_number'],
            'gross_number' => $info['gross_number'],
            'update_time' => date('Y-m-d H:i:s',time())
        ];
        $res = $this->table->update('equipment',['id'=>$info['id']],$data);
        if(!$res){
            return $this->outMessage(-1,'保存失败');
        }
        return $this->outMessage(10000,'保存成功');
    }



    /**
     * 删除
     */
    public function api_archives_equipment_delete($msg)
    {
        $id = isset($msg['data']['id'])?$msg['data']['id']:'';
        if(empty($id)){
            return $this->outMessage(-1,'ID不能为空');
        }
        $res = $this->table->delete('equipment',['id'=>$id]);
        if(!$res){
            return $this->outMessage(-1,'删除失败');
        }
        return $this->outMessage(10000,'删除成功');
    }
 

}
